﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Sales;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Sales
{
    public partial class frmMaintainSalesQuoteOrderQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private SalesQuoteOrderEntity mSelectedQuoteOrder;
        public SalesQuoteOrderEntity SelectedQuoteOrder
        {
            get { return mSelectedQuoteOrder; }
        }
        #endregion

        #region Constructor
        public frmMaintainSalesQuoteOrderQuery()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);
            cmbStatus.SetSelectedValue(NormalStatus.STATUS03.ToString());

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCURRENCY, "CS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                MstCustomerEntity customer = (MstCustomerEntity)txtCustomer.Tag;
                condition += " AND QO.CUSTOMER_ID = '" + customer.CustomerId + "' ";
            }
            else
            {
                condition += @" AND QO.CUSTOMER_ID IN (SELECT CUSTOMER_ID FROM MST_PERSON_CUSTOMER PC
						LEFT JOIN MST_PERSON P ON P.PERSON_ID = PC.PERSON_ID
                        LEFT JOIN MST_PERSON P2 ON P2.PERSON_ID = P.SUPERVISOR
						LEFT JOIN MST_PERSON P3 ON P3.PERSON_ID = P2.SUPERVISOR
						LEFT JOIN MST_PERSON P4 ON P4.PERSON_ID = P3.SUPERVISOR
						LEFT JOIN MST_PERSON P5 ON P5.PERSON_ID = P4.SUPERVISOR
                        WHERE PC.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + @"'
	                    OR P2.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + @"'
	                    OR P3.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + @"'
	                    OR P4.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + @"'
	                    OR P5.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + @"')";
            }
            if (string.IsNullOrEmpty(txtQuoteCode.Text) == false)
            {
                condition += " AND QO.QUOTE_ORDER_CODE LIKE '%" + txtQuoteCode.Text + "%'";
            }
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity entity = (MstDepartmentEntity)txtCustomer.Tag;
                condition += " AND QO.QUOTE_DEPARTMENT = '" + entity.DepartmentId + "' ";
            }
            if (txtQuotePerson.Tag != null && txtQuotePerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtQuotePerson.Tag;
                condition += " AND QO.QUOTE_PERSON = '" + entity.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND QO.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (dtpQuoteDate.EditValue!=null)
            {
                condition += " AND QO.QUOTE_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpQuoteDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpQuoteDateTo.EditValue!=null)
            {
                condition += " AND QO.QUOTE_DATE <= CONVERT(DATETIME,'" + ((DateTime)dtpQuoteDateTo.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "QUOTE_ORDER_CODE DESC";
            DataTable dt = SalesQuoteOrderBs.GetByCondition(condition, orderBy, "");
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
            base.OnShown(e);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedQuoteOrder = new SalesQuoteOrderEntity(dr);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (mSelectedQuoteOrder != null)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("请选择一个销售报价单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedQuoteOrder != null)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("请选择一个销售报价单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedCustomer != null)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                txtCustomer.Tag = frm.SelectedCustomer;
            }
            else
            {
                txtCustomer.Text = "";
                txtCustomer.Tag = null;
            }
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtDepartment.Text = frm.SelectedDepartment.DepartmentName;
                txtDepartment.Tag = frm.SelectedDepartment;
            }
            else
            {
                txtDepartment.Text = "";
                txtDepartment.Tag = null;
            }
        }

        private void txtQuotePerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtQuotePerson.Text = frm.SelectedPerson.NameCn;
                txtQuotePerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtQuotePerson.Text = "";
                txtQuotePerson.Tag = null;
            }
        }

        #endregion

    }
}
